package com.yzl.frelink.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;

import org.json.JSONArray;
import org.json.JSONObject;

import com.yzl.frelink.entities.Fretish;


public class JsonReader {
    public static Fretish[] parseFromFile(String filePath) throws Exception {
        Commander.getFretishFromJson(filePath);
        File file = new File(filePath.replace("reqs_json", "out_json").replace("uploads", "out_json"));
        Reader reader = new InputStreamReader(new FileInputStream(file), "UTF-8");
        int ch = 0;
        StringBuffer sb = new StringBuffer();
        while ((ch = reader.read()) != -1) {
            sb.append((char) ch);
        }
        reader.close();

        String jsonStr = sb.toString();
        JSONArray jsonArray = new JSONArray(jsonStr);
        Fretish[] requirements = new Fretish[jsonArray.length()];
        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            Fretish requirement = parseRequirement(jsonObject);
            requirements[i] = requirement;
            // System.out.println(jsonObject);
            // System.out.println(requirement);
        }
        return requirements;
    }

    private static Fretish parseRequirement(JSONObject jsonObject) {
        Fretish requirement = new Fretish();

        requirement.setReqid(jsonObject.getString("reqid"));
        requirement.setContent(jsonObject.getString("fulltext"));

        JSONObject semantics = jsonObject.getJSONObject("semantics");
        // scope
        JSONObject scope = semantics.getJSONObject("scope");
        if (scope.has("type") && !scope.getString("type").equals("null")) {
            requirement.setScopeType(scope.getString("type"));
            requirement.setScope(semantics.getString("scope_mode"));
        }
        // condition
        if (semantics.has("qualifier_word") && !semantics.getString("qualifier_word").equals("null")) {
            requirement.setConditionType(semantics.getString("qualifier_word"));
            requirement.setCondition(semantics.getString("pre_condition"));
        }
        // component
        requirement.setComponent(semantics.getString("component_name"));
        // timing
        if (semantics.has("timing") && !semantics.getString("timing").equals("null")) {
            requirement.setTimingType(semantics.getString("timing"));
        }
        // response
        requirement.setResponse(semantics.getString("post_condition"));
        // variables
        JSONArray variables = semantics.getJSONArray("variables");
        String[] vars = new String[variables.length()];
        for (int i = 0; i < variables.length(); i++) {
            vars[i] = variables.getString(i);
        }
        requirement.setVariables(vars);
        requirement.splitVars();
        return requirement;
    }

    public static JSONObject readJson(String filePath) throws IOException {
        File file = new File(filePath);
        Reader reader = new InputStreamReader(new FileInputStream(file), "UTF-8");
        int ch = 0;
        StringBuffer sb = new StringBuffer();
        while ((ch = reader.read()) != -1) {
            sb.append((char) ch);
        }
        reader.close();

        String jsonStr = sb.toString();
        return new JSONObject(jsonStr);
    }
}
